package com.demo.usermanager.controller;

import com.demo.usermanager.model.UserInfo;
import com.demo.usermanager.service.UserService;
import com.demo.usermanager.util.ConstVariable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 登录功能
     * @param loginname
     * @param password
     * @return
     */
    @RequestMapping("/login")
    public boolean login(HttpServletRequest request,String loginname, String password){
        if (StringUtils.hasLength(loginname) && StringUtils.hasLength(password)) {
            // 参数有效
            UserInfo userInfo= userService.login(loginname, password);
            if (userInfo != null && userInfo.getUid() > 0) {
                // 存储session
                HttpSession session=request.getSession(true);
                session.getAttribute(ConstVariable.USER_SESSION_KEY);
                return  true;
            }
        }
        return false;
    }

    // 查询所有列表(无分页)
    @RequestMapping("/list")
    public List<UserInfo> getAll(){
        return userService.getAll();
    }

    /**
     * 添加用户
     * @param userInfo
     * @param request
     * @return
     */
    @RequestMapping("/add")
    public int add(UserInfo userInfo,HttpServletRequest request){
        int result=0;
        // 1.非空校验
        if(userInfo==null||!StringUtils.hasLength(userInfo.getUsername())||
        !StringUtils.hasLength(userInfo.getLoginname())||
        !StringUtils.hasLength(userInfo.getPassword())) return  result;
        // 2.必须为超级管理员，才能进行添加操作
        HttpSession session=request.getSession(false);
        if(session==null || session.getAttribute(ConstVariable.USER_SESSION_KEY)==null ) return result;
        UserInfo loginUser=(UserInfo)session.getAttribute(ConstVariable.USER_SESSION_KEY);
        if(!loginUser.isIsadmin()) return result;
        // 3.对登录名做唯一校验
        UserInfo loginNameUser=userService.getUserByLoginName(userInfo.getLoginname());
        // 当前登录名是否存在的判断
        if(loginNameUser!=null&&loginNameUser.getUid()>0) return result;
        // 4.添加用户到数据库
        result=userService.add(userInfo);
        return  result;
    }

    @RequestMapping("/getuserbyuid")
    public UserInfo getUserByUid(Integer uid){
        UserInfo userInfo=null;
        // 1.得空校验
        if(uid==null||uid<=0) return userInfo;
        // 2.查询数据库
        userInfo=userService.getUserByUid(uid);
        // 3.将密码隐藏
        userInfo.setPassword("");
        return userInfo;
    }

    @RequestMapping("/update")
    public int update(UserInfo userInfo,HttpServletRequest request){
        int result=0;
        // 1.非空校验
        if(userInfo==null||userInfo.getUid()<=0||
                !StringUtils.hasLength(userInfo.getUsername())) return  result;
        // 2.必须为超级管理员，才能进行添加操作
        HttpSession session=request.getSession(false);
        if(session==null || session.getAttribute(ConstVariable.USER_SESSION_KEY)==null ) return result;
        UserInfo loginUser=(UserInfo)session.getAttribute(ConstVariable.USER_SESSION_KEY);
        if(!loginUser.isIsadmin()) return result;
        // 3.执行修改操作
        result=userService.update(userInfo);
        return  result;
    }
}
